<template>
  <q-page class="cc-admin row">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table
        flat
        color="primary"
        class="cross_table"
        separator="cell"
        :columns="columns"
        :data="list"
        row-key="id"
        :pagination.sync="pagination"
        :visible-columns="group"
        @request="query"
        :rows-per-page-options="[10,20,50,100]"
        :loading="loading"
        selection="multiple"
        :selected.sync="selected"
      >
        <template v-slot:top>
          <div class="row no-wrap full-width">
            <q-input
              clearable
              outlined
              dense
              placeholder="请输入关键字搜索"
              class="on-left"
              @input="query"
              debounce="500"
              v-model="key"
            >
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />
              <q-btn icon="mdi-restore" no-wrap color="primary" label="刷新商品" @click="refresh" />
            </div>
          </div>
        </template>
        <template #body-cell-is_del="props">
          <q-td  key="is_del" :props="props">{{getGoodsLabel(props.row.is_del) }}</q-td>
        </template>

        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="edit2(props.row,'detail')">
              <q-tooltip>编辑</q-tooltip>
            </q-btn>
            <btn-del label="商品类型管理" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form  @submit="onSubmit" class="dialog_card column" @reset="onReset">
        <h5 class="view_title justify-between q-px-md">
          {{editType}}商品类别管理
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-9">
              <h5>
                <q-icon name="star" color="red" />类别ID：
              </h5>
              <q-input outlined dense disable v-model="form.id" type="text" :rules="[requiredTest]" />
            </div>
            <div class="col-9">
              <h5>
                <q-icon name="star" color="red" />类别名称：
              </h5>
              <q-input outlined dense v-model="form.name" type="text" :rules="[requiredTest]" />
            </div>
            <div class="col-9">
              <h5>
                类别描述：
              </h5>
              <q-input outlined dense v-model="form.descript" type="text"  />
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-sm" color="primary"  icon="mdi-check-bold" label="提交" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import { requiredTest } from 'boot/inputTest';
import catalogselect from 'components/catalogselect';
import viewcatalog from 'components/viewcatalog';

export default {
  name: 'MntDeploy',
  mixins: [IndexMixin],
  components: {
    confirm,
    catalogselect,
  },
  data() {
    return {
      description:'aaa',
      form: {
        id:'',
        name:'',
        parent_id:'',
        keywords:'',
        img: '',
        sort:'',
        description: '描述',
        is_share: 0,
      },
      columns: [
        { name: 'id', align: 'center', label: '序号', field: 'id', },
        {
          name: 'name', align: 'left', label: '名称', field: 'name',
        },
        {
          name: 'keywords', align: 'center', label: '关键字', field: 'keywords',
        },
        {
          name: 'create_time', align: 'left', label: '添加时间', field: 'create_time',
        },
        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.BASE_URL}/uploadfile`,
      imgUrl: `http://img.tel.gzzsu.com`,
      url: {
        list: '/categorylist',
        add: '/categoryadd',
        edit: '/categoryupdate',
        delete: '/categorydelete',
        detail: '/categorydetail',
        category: '/categorylist',
        deleteBatch: '/client/deleteBatch',
        exportXlsUrl: '/client/exportXls',
        importExcelUrl: '/client/importExcel',
      },
      isEdit: 0,
      modelList: [],
    };
  },
  methods: {
    refresh(){
      this.$axios.post('/categorylist', {}).then((r) => {
        this.list = r.data;
      });
    },
    requiredTest,
    addBefore() {
      this.form.catalogId = this.catalog;
      return true;
    },
    selectCatalog(n) {
      this.catalog = n;
      this.query();
    },
    selectFiles() {
      this.$refs.fileUploader.pickFiles();
    },
    importAvatar() {
      this.$refs.avatarUploader.pickFiles();
    },
    importedAvatar({ xhr }) {
      const response = JSON.parse(xhr.responseText);
      if (response.code == 0) {
        this.form.ad_img = response.data.url
        console.log(this.form.ad_img)
        this.$q.notify('上传成功');
      } else {
        this.$q.notify({
          color: 'red',
          message: '上传失败',
        });
      }
      this.$refs.avatarUploader.removeUploadedFiles();
    },
    uploadedFiles({ xhr }) {
      const response = JSON.parse(xhr.responseText);
      if (response.success) {
        this.$q.notify('上传成功');
        this.queryImages();
      } else {
        this.$q.notify({
          color: 'red',
          message: '上传失败',
        });
      }
      this.$refs.fileUploader.removeUploadedFiles();
    },
    imagePath(src) {
      let path = '';
      if (src) {
        if (src.startsWith('http')) {
          return src;
        }
        path = `${this.imgUrl}/${src}`;
      }
      return path;
    },
    queryImages() {
      // this.$axios.get(`/sys/file/listByMainId?mainId=${this.form.id}`).then(({ result }) => {
      //   this.attachments = result;
      // });
    },
    getGoodsLabel(status){
      const list = {
        menuType: [0 , 1, 2 ],
        icon: ['正常', '删除' ],
      };
      const showStatus = ['正常', '删除' ];
      return showStatus[status]
    },
    initDict() {
      /*this.$axios.get('/sys/dictItem/selectItemsByDefId?defId=mntApp').then((r) => {
        this.mntApp = r;
      });
      this.$axios.get('/sys/dictItem/selectItemsByDefId?defId=mntServer').then((r) => {
        this.mntServer = r;
      });*/
    },
    deploy() {
      this.$refs.keyFileUploader.pickFiles();
    },
    importedDeploy({ xhr }) {
      this.$refs.keyFileUploader.removeUploadedFiles();
      const { response } = xhr;
      const res = JSON.parse(response);
      if (res.success) {
        this.form.keyFile = res.message;
      } else {
        this.$error(res.message);
      }
    },
    sysRestore() {
      this.$refs.sysRestore.dialog = true;
    },


    onSubmit (evt) {
      //this.form.isEdit = this.isEdit
      console.log( this.form  )
      let url = ''
      if(this.form.store_nums < 0){
        this.$q.notify({
          color: 'red',
          message: '商品库存不能小于0',
        });
        return
      }else{
        if(this.isEdit == 1){
          url = this.url.edit
        }else{
          url = this.url.add
        }
        this.$axios.post(url,this.form).then((r) => {
          this.babyWord = r;
          if(r.success = true){
            this.$q.notify({ position: 'top', color: 'info', textColor: 'white', icon: 'cloud_done', group: false, html: true, message: '保存成功！！',});
            this.onReset()
            this.refresh()
            this.$refs.dialog.hide();
          }else{
            this.$q.notify({ position: 'top', color: 'error', textColor: 'red', icon: 'cloud_done', group: false, html: true, message: '保存失败！！',});
          }
        });
      }

      //evt.target.submit()
    },
    onReset(){
      this.form = {
        name : '' ,
        mobile : '',
        telephone : '',
        tag : '',
        remark : '',
      }
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus"></style>
